﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Dashboard.ProductBonus.Business;
using System.Globalization;

namespace Dashboard.ProductBonus
{
    public partial class FormWorkingShiftAdd : Form
    {
        public FormWorkingShiftAdd()
        {
            InitializeComponent();
        }

        private void toolStripButtonSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.textBoxCode.Text.Trim()))
            {
                MessageBox.Show(this, "Please input the Name field.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.textBoxCode.Focus();
                return;
            }

            if (this.textBoxFromTime.Text.Trim() == ":")
            {
                MessageBox.Show(this, "Please input the [From Time] field.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.textBoxFromTime.Focus();
                return;
            }

            if (this.textBoxToTime.Text.Trim() == ":")
            {
                MessageBox.Show(this, "Please input the [To Time] field.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.textBoxToTime.Focus();
                return;
            }

            try
            {
                WorkingShift ws = new WorkingShift();

                string code = this.textBoxCode.Text.Trim();
                string description = this.textBoxDescription.Text.Trim();
                DateTime fromTime = DateTime.ParseExact(this.textBoxFromTime.Text, "HH:mm", new CultureInfo("en-US"));
                DateTime toTime = DateTime.ParseExact(this.textBoxToTime.Text, "HH:mm", new CultureInfo("en-US"));

                if (ws.CalculateTotalTime(fromTime, toTime) <= 2)
                {
                    if (MessageBox.Show(this, "Total time is smaller than 2 hours, are you sure to continue?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No)
                    {
                        return;
                    }
                }

                // Active状态的Code唯一
                if (ws.IsWorkingShiftExists(code))
                {
                    MessageBox.Show(this, "Working shift already exists, please change the code to another one!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.textBoxCode.Focus();
                    return;
                }

                // 按照Code+FromTime+ToTime找是否存在Invalid的资料，如果存在，则直接启用原来的资料
                bool isValid = false;
                DataTable result = ws.GetWorkingShift(0, code, fromTime, toTime, isValid);
                if (result.Rows.Count > 0)
                {
                    ws.UpdateWorkingShift(int.Parse(result.Rows[0]["Id"].ToString()), description, fromTime, toTime, true);
                }
                else
                {
                    ws.AddNewWorkingShift(code, description, fromTime, toTime, true);
                }

                MessageBox.Show(this, "Save successfully!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);

                this.InitUI();
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void InitUI()
        {
            this.textBoxCode.Text = string.Empty;
            this.textBoxDescription.Text = string.Empty;
            this.textBoxFromTime.Text = string.Empty;
            this.textBoxToTime.Text = string.Empty;

            this.textBoxCode.Focus();
        }

        private void toolStripButtonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }
    }
}
